--[[
	gSpeak
	Author: Adult
]]--

local voice = {}

local function requestJoin( id )
	if not channelExists( id ) then
		return false, "Channel does not exist"
	end

	net.Start( VOICE_REQJOIN )
		net.WriteString( id )
	net.SendToServer()
end

local function leave( id )
	if not channelExists( id ) then
		return false, "Channel does not exist"
	end

	net.Start( VOICE_LEAVE )
		net.WriteString( id )
	net.SendToServer() 
end

--[[
	Net Handlers
]]--

net.Receive( VOICE_MESSAGE, function() 
	local data = net.ReadTable()

	if data.usederma then
		Derma_Message( data.msg, "gSpeak", "Dismiss" )
	end
	
	chat.AddText( "[gSpeak] " .. data.msg )
end)

-- probably wondering why i do this.
-- it's so clientside, you can't modify anything
-- Makes it read only, essentially.
net.Receive( VOICE_DATA, function()
	voice = net.ReadTable()

	-- Just make sure it's valid 
	voice = voice or {}
end)


--[[
	Voice handling
	The thing that actually restricts the voices
]]--

local localply = LocalPlayer() 

local function playerStartVoice( ply )
	local localch = localply:getGSpeakChannel()

	if not localch then return end

	local plych = ply:getGSpeakChannel()
	if localch ~= plych then
		-- A way to guarantee they stay muted.
		ply:SetMuted( true )
		return false 
	else 
		ply:SetMuted( false )
		return true 
	end 
end

hook.Add( "PlayerStartVoice", "GSpeak_startvoice", playerStartVoice )